/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pbpm.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itttq.gshop.domain.po.UserFavoritesPo;
import com.itttq.gshop.domain.po.WareInfoPo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.pbpm.mapper.IUserFavoritesMapper;
import com.itttq.gshop.pbpm.mapper.IWareInfoMapper;

@Service
public class UserFavoritesService {
	@Autowired
	private IUserFavoritesMapper userFavoritesMapper;
	@Autowired
	private IWareInfoMapper wareInfoMapper;
	
	/**
	 * 根据ID获取UserFavoritesPo对象
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return UserFavoritesPo
	 */
	public UserFavoritesPo getUserFavorites(int id) {
		return userFavoritesMapper.findUserFavoritesById(id);
	}
	
	/**
	 * 根据条件获取UserFavoritesPo对象，并返回查询的数据总数
	 * 
	 * @param userFavorites
	 * @Author 迷蝶
	 * @return ResultPageTo<UserFavoritesPo>
	 */
	public ResultPageTo<UserFavoritesPo> getUserFavoritess(UserFavoritesPo userFavorites) {
		return new ResultPageTo<UserFavoritesPo>(userFavorites.getStart(), userFavorites.getSize(), 
				userFavoritesMapper.findUserFavoritesCount(userFavorites), userFavoritesMapper.findUserFavoritess(userFavorites));
	}
	
	/**
	 * 根据判断是否收藏商品
	 * 
	 * @param userId	用户ID
	 * @param wareId	商品ID
	 * @return
	 * @Author 迷蝶
	 * @return boolean	如果已关注则为true；反之为false
	 */
	public Integer isFavoritess(UserFavoritesPo fav) {
		return userFavoritesMapper.findUserFavoritesCount(fav);// > 0 ? true : false;
	}
	
	/**
	 * 根据ID删除UserFavoritesPo对象
	 * 
	 * @param id
	 * @Author 迷蝶
	 */
	public Integer delUserFavorites(int id) {
		return userFavoritesMapper.delUserFavoritesById(id);
	}
	
	/**
	 * 增加UserFavoritesPo对象
	 * 
	 * @param userFavorites
	 * @Author 迷蝶
	 */
	public Integer addUserFavorites(UserFavoritesPo userFavorites) {
		return userFavoritesMapper.addUserFavorites(userFavorites);
	}
	
	/**
	 * 根据条件更新UserFavoritesPo对象
	 * 
	 * @param userFavorites
	 * @Author 迷蝶
	 */
	public Integer updateUserFavorites(UserFavoritesPo userFavorites) {
		return userFavoritesMapper.updateUserFavorites(userFavorites);
	}
	
	/**
	 * 收藏关注商品
	 * 
	 * @param wareId	商品ID
	 * @param userId		用户ID
	 * @param username	用户名称
	 * @Author 迷蝶
	 * @return Integer	影响的行数
	 */
	public Integer favorite(int wareId, int userId, String username) {
		UserFavoritesPo favorites = new UserFavoritesPo();
		favorites.setUserId(userId);
		favorites.setUserName(username);
		// 获取商品信息
		WareInfoPo ware = wareInfoMapper.findWareInfoById(wareId);
		favorites.setWareId(wareId);
		favorites.setWareName(ware.getName());
		favorites.setMainImage(ware.getImageMain());
		favorites.setPriceOriginal(ware.getPriceOriginal());
		favorites.setPricePromotional(ware.getPricePromotional());
		//TODO GXG 判断是否已经关注收藏过
		return userFavoritesMapper.addUserFavorites(favorites);
	}
	
	/**
	 * 取消收藏关注商品
	 * 
	 * @param wareId	商品ID
	 * @param userId		用户ID
	 * @Author 迷蝶
	 * @return Integer	影响的行数
	 */
	public Integer unFavorite(int wareId, int userId) {
		return userFavoritesMapper.delFavoritesByUserId(wareId, userId);
	}
	
	/**
	 * 批量取消收藏关注商品
	 * 
	 * @param userId		用户ID，作为条件使用，避免误删
	 * @param ids			关注记录ID集合，多个以逗号分隔
	 * @Author 迷蝶
	 * @return Integer
	 */
	public Integer unFavoriteBatch(int userId, String ids) {
		return userFavoritesMapper.delFavoriteByIds(userId, ids);
	}
	
}
